<template>
  <div style="text-align: center;">
    <h2>欢迎 {{ userInfo.name }} 光临用户中心</h2>
    <div>
      <h3>
        您的身份：
        <el-tag v-if="userInfo.stype === '1'" effect="dark" type="warning">学生</el-tag>
        <el-tag v-else-if="userInfo.stype === '2'" effect="dark" type="success">管理员</el-tag>
        <el-tag v-else>未知身份</el-tag>
      </h3>
    </div>

    <el-form :model="passForm" status-icon :rules="rules" ref="passForm" label-width="100px">
      <el-form-item label="旧密码" prop="currentPass">
        <el-input type="Password" v-model="passForm.currentPass" autocomplete="off" clearable></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="Password">
        <el-input type="Password" v-model="passForm.Password" autocomplete="off" clearable></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="Password" v-model="passForm.checkPass" autocomplete="off" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="submitForm('passForm')" icon="el-icon-check">提交</el-button>
        <el-button type="info" @click="resetForm('passForm')" icon="el-icon-close">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import "../router/index"
import index from "@/router";
export default {
  name: "UserCenter",
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.passForm.Password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      userInfo: {
        UserId: '',
        sname: '',
        ssex: '',
        Password: '',
        major_name: '',
        college_name: '',
        grade: '',
        stype: '',
      },
      passForm: {
        Password: '',
        checkPass: '',
        currentPass: ''
      },
      students : {
        UserId: '',
        password: '',
        checkPass: '',
        currentPass: ''
      },
      rules: {
        Password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        currentPass: [
          { required: true, message: '请输入当前密码', trigger: 'blur' },
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      // this.$axios.get("/userCenter/userInfo").then(res => {
      //
      //   this.userInfo = res.data.data;
      // })
      let user = JSON.parse(localStorage.getItem("User2"));
      console.log("user2 = ", user)
      this.userInfo.UserId = user.UserId;
      this.userInfo.sname = user.sname;
      this.userInfo.ssex = user.ssex;
      this.userInfo.Password = user.Password;
      this.userInfo.major_name = user.major_name;
      this.userInfo.college_name = user.college_name;
      this.userInfo.grade = user.grade;
      this.userInfo.stype = user.stype;
      this.passForm.currentPass = this.userInfo.password
      console.log("this.userInfo : ", this.userInfo);
      console.log("current : ", this.passForm.currentPass);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {

          const _this = this
          this.students.UserId = this.userInfo.UserId
          this.students.currentPass = this.passForm.currentPass
          this.students.checkPass = this.passForm.checkPass
          this.students.password = this.passForm.Password
          this.$axios.post('api/userCenter/changePassword', {...this.students}).then(res => {
            console.log("修改的res", res)
            if (res.data.msg == "修改密码成功") {
              localStorage.clear();
              sessionStorage.clear();
              this.$store.commit("resetState");
              this.$router.push("/login");
            }

          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.el-form {
  width: 420px;
  margin: 50px auto;
}
</style>